package com.example.jack.community;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.jack.community.data.HomePhotoConfig;
import com.example.jack.community.view.community.PublishPopWindow;
import com.example.jack.community.view.dynamic.DynamicFragment;
import com.example.jack.community.view.home.HomeFragment1;
import com.example.jack.community.view.me.MeFragment;
import com.example.jack.community.view.me.util.DemoIntentService;
import com.example.jack.community.view.me.util.DemoPushService;
import com.example.jack.community.view.news.NewsFragment;
import com.igexin.sdk.PushManager;

//首页
public class MainActivity extends BaseActivity {
    private static String TAG = "TestLog";
    private RadioGroup BottomNavigation;
    private RadioButton mButton;
    private CheckBox communityBox;
    private HomeFragment1 homeFragment;
    private DynamicFragment dynamicFragment;
    private NewsFragment newsFragment;
    private MeFragment meFragment;
    private FragmentManager fm;
    private Fragment mCurrentFragment;

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GeTui();//个推
        Test_Share();
        getDeviceDensity(); //用于获取设备宽高，在首页九宫格使用到
        initBottom();
        fm = getSupportFragmentManager();
    }

    /*
      测试sharePreferences取出用户数据
     */
    private void Test_Share() {
        SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
        String tocken = sharedPreferences.getString("token", "tocken取出失败");
        String telephone = sharedPreferences.getString("telephone", "telephone取出失败");
        Log.d(TAG, "MainActivity: 数据取出成功 token:" + tocken + "  telephone:" + telephone);
    }

    private void GeTui() {
        // com.getui.demo.DemoPushService 为第三方自定义推送服务
        PushManager.getInstance().initialize(this.getApplicationContext(), DemoPushService.class);
        // com.getui.demo.DemoIntentService 为第三方自定义的推送服务事件接收类
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);
    }

    /**
     * 获取当前设备的屏幕密度等基本参数
     */
    protected void getDeviceDensity() {
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        HomePhotoConfig.EXACT_SCREEN_HEIGHT = metrics.heightPixels;
        HomePhotoConfig.EXACT_SCREEN_WIDTH = metrics.widthPixels;
    }

    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        homeFragment = homeFragment.newInstance(getString(R.string.Home));
        transaction.replace(R.id.main_container, homeFragment);
        transaction.commit();
        mCurrentFragment = homeFragment;
    }


    private void initBottom() {
        BottomNavigation = (RadioGroup) findViewById(R.id.main_radio_group);
        BottomNavigation.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //  FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();
                hideAllFragment(transaction);
                switch (checkedId) {
                    case R.id.bottom_home:

                        if (homeFragment == null) {
                            Log.d(TAG, "homeFragment初始化");
                            homeFragment = homeFragment.newInstance(getString(R.string.Home));
                            transaction.add(R.id.main_container, homeFragment);
                            mCurrentFragment = homeFragment;
                        } else {
                            mCurrentFragment = homeFragment;
                            transaction.show(homeFragment);
                        }
                        Log.d(TAG, "homeFragment替换");
                        transaction.addToBackStack(null);
                        // transaction.replace(R.id.main_container, homeFragment);

                        break;
                    case R.id.bottom_dynamic:
                        if (dynamicFragment == null) {
                            Log.d(TAG, "dynamicFragment初始化");
                            dynamicFragment = dynamicFragment.newInstance("活动");
                            transaction.add(R.id.main_container, dynamicFragment);
                            mCurrentFragment = dynamicFragment;
                        } else {
                            mCurrentFragment = dynamicFragment;
                            transaction.show(dynamicFragment);

                        }
                        Log.d(TAG, "dynamicFragment替换");
                        //           transaction.replace(R.id.main_container, dynamicFragment);
                        transaction.addToBackStack(null);
                        break;
                    case R.id.bottom_news:
                        if (newsFragment == null) {
                            Log.d(TAG, "dynamicFragment初始化");
                            newsFragment = newsFragment.newInstance("活动");
                            transaction.add(R.id.main_container, newsFragment);
                            mCurrentFragment = newsFragment;
                        } else {
                            mCurrentFragment = newsFragment;
                            transaction.show(newsFragment);
                        }
                        Log.d(TAG, "newsFragment替换");
                        //           transaction.replace(R.id.main_container, dynamicFragment);
                        transaction.addToBackStack(null);
                        break;
                    case R.id.bottom_me:
                        if (meFragment == null) {
                            Log.d(TAG, "dynamicFragment初始化");
                            meFragment = meFragment.newInstance("活动");
                            transaction.add(R.id.main_container, meFragment);
                            mCurrentFragment = meFragment;
                        } else {
                            mCurrentFragment = meFragment;
                            transaction.show(meFragment);
                        }
                        Log.d(TAG, "meFragment替换");
                        //           transaction.replace(R.id.main_container, dynamicFragment);
                        transaction.addToBackStack(null);
                        break;
                    default:
                        if (homeFragment == null) {
                            Log.d(TAG, "dynamicFragment初始化");
                            homeFragment = homeFragment.newInstance("活动");
                            transaction.add(R.id.main_container, homeFragment);
                        } else {
                            transaction.show(homeFragment);
                        }
                        Log.d(TAG, "homeFragment替换");
                        //           transaction.replace(R.id.main_container, dynamicFragment);
                        break;
                }
                transaction.commit();
            }
        });
        setDefaultFragment();
        communityBox = (CheckBox) findViewById(R.id.bottom_community);
        communityBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PublishPopWindow popWindow = new PublishPopWindow(MainActivity.this);
                popWindow.showMoreWindow(v);
            }
        });
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (homeFragment != null) fragmentTransaction.hide(homeFragment);
        if (dynamicFragment != null) fragmentTransaction.hide(dynamicFragment);
        if (newsFragment != null) fragmentTransaction.hide(newsFragment);
        if (meFragment != null) fragmentTransaction.hide(meFragment);
    }

//    @Override
//public boolean onKeyDown(int keyCode, KeyEvent event) {
//    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN){
//        if(mCurrentFragment==homeFragment){
//            onBackPressed();
//            System.exit(0);
//        }else if(mCurrentFragment == dynamicFragment){
//            onBackPressed();  System.exit(0);
//        }else if(mCurrentFragment == newsFragment){
//            onBackPressed();  System.exit(0);
//        }else{
//            onBackPressed();  System.exit(0);
//        }
//    }
//    return super.onKeyDown(keyCode, event);
//}

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            onBackPressed();
        }
        return super.onKeyDown(keyCode, event);
    }

    //重写onBackPressed实现按下物理back键退出程序切换后台但是不杀死程序
    @Override
    public void onBackPressed() {
        Intent i = new Intent(Intent.ACTION_MAIN);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.addCategory(Intent.CATEGORY_HOME);
        startActivity(i);
    }
}